package logic

import (
	"context"
	"time"

	"mall/common/enumx"
	"mall/service/blog/model"
	"mall/service/blog/rpc/internal/svc"
	"mall/service/blog/rpc/types/blog"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchLogLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSearchLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchLogLogic {
	return &SearchLogLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SearchLogLogic) SearchLog(in *blog.SearchLogReq) (*blog.SearchLogResp, error) {
	var searchLogId int64
	if in.UserId > 0 && in.PlatformId > 0 && in.Keyword != "" {
		searchLogId = l.svcCtx.BlogUser.InsertSearchLog(l.ctx, &model.BlogUserSearchLog{
			UserID:     uint64(in.UserId),
			PlatformID: uint(in.PlatformId),
			Keyword:    in.Keyword,
			Status:     uint8(enumx.ObjectStatusAproved),
			CreatedAt:  time.Now(),
		})
	}

	return &blog.SearchLogResp{
		SearchLogId: searchLogId,
	}, nil
}
